from django.shortcuts import render
from rest_framework.viewsets import ModelViewSet, GenericViewSet
from rest_framework.mixins import ListModelMixin, CreateModelMixin, DestroyModelMixin
from rest_framework.permissions import IsAuthenticated
from rest_framework.pagination import PageNumberPagination
from rest_framework.response import Response
from rest_framework.status import HTTP_201_CREATED, HTTP_200_OK
from django.db.models import F
# Create your views here.
from community.models import Article
from community.serializers import *
from utils.customPermissions import auto_user


# 分页类
class ArticlePageNumberPagination(PageNumberPagination):
    page_size = 10  # 每页显示最大条数
    page_size_query_param = 'size'
    max_page_size = 100


# 文章类
class ArticleViewSet(ModelViewSet):
    queryset = Article.objects.filter(is_delete=False, status=1)  # 获取is_delete=False,和已发布的文章
    serializer_class = ArticleSerializer
    permission_classes = [IsAuthenticated]  # 内置登录权限
    pagination_class = ArticlePageNumberPagination  # 只需要指定一下分页的类,就可以实现分页了

    @auto_user
    def create(self, request, *args, **kwargs):  # 老三步
        serializer = self.get_serializer(data=request.data)  # 反序列化一下前端传来的数据
        serializer.is_valid(raise_exception=True)  # 校验一下,失败抛出异常
        serializer.save()  # 最后保存一下
        return Response(serializer.data, status=HTTP_201_CREATED)

    @auto_user
    def update(self, request, *args, **kwargs):
        # 用当前登录用户和当前文章的id去数据库进行搜索,没有找到数据,则说明当前文章不是当前用户创建的
        if not Article.objects.filter(id=int(kwargs['pk']), user=request.user):
            return Response({'msg': '你没有修改的权限'})
        # 选择修改接口还是创建接口差异就在于有没有实例(instance),像上面的创建接口就没有实例,但是修改需要实例
        article = Article.objects.get(id=int(kwargs['pk']))
        serializer = self.get_serializer(article, data=request.data)
        serializer.is_valid(raise_exception=True)
        serializer.save()
        return Response(request.data, status=HTTP_200_OK)

    def destroy(self, request, *args, **kwargs):
        if not Article.objects.filter(id=int(kwargs['pk']), user=request.user):
            return Response({'msg': '你没有删除的权限'})

        return ModelViewSet.destroy(self, request, *args, **kwargs)

    # 浏览量自增
    def retrieve(self, request, *args, **kwargs):
        Article.objects.filter(id=int(kwargs['pk'])).update(page_view=F('page_view') + 1)
        return ModelViewSet.retrieve(self, request, *args, **kwargs)


# 评论区类
class CommentViewSet(GenericViewSet, ListModelMixin, CreateModelMixin, DestroyModelMixin):
    queryset = Comment.objects.filter(level=1)  # 拿到所有
    # serializer_class = CommentSerializer  # 拿到序列化后的数据
    serializer_class = CommentHandlerSerializer  # 拿到序列化后的数据
    permission_classes = [IsAuthenticated]  # 登录权限

    # @auto_user  # 不管是谁,自动把你换成当前登录用户
    def create(self, request, *args, **kwargs):
        return CreateModelMixin.create(self, request, *args, **kwargs)  # 直接调用create的Mixin类

    # 谁创建的谁才能删
    def destroy(self, request, *args, **kwargs):
        if not Comment.objects.filter(id=int(kwargs['pk']), user=request.user):
            return Response({'msg': '您没有删除权限'})

        return DestroyModelMixin.destroy(self, request, *args, **kwargs)

        # def comment(self, request, *args, **kwargs):


